package com.timer.taskRule.controller;

import cn.hutool.aop.ProxyUtil;
import cn.hutool.aop.proxy.CglibProxyFactory;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.timer.taskRule.entity.TaskRule;
import com.timer.taskRule.entity.User;
import com.timer.taskRule.exception.TaskException;
import com.timer.taskRule.mapper.InsertMapper;
import com.timer.taskRule.mapper.taskRuleMapper;
import com.timer.taskRule.service.TaskService;
import com.timer.taskRule.service.impl.TaskServiceImpl;
import com.timer.taskRule.utils.RandomData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Controller
@RequestMapping("/taskRule")
@ResponseBody
public class taskRuleController {

    @Autowired
    private taskRuleMapper taskRuleMapper;

    @Autowired
    private TaskService taskService;

    @Autowired
    private InsertMapper insertMapper;

    CountDownLatch countDownLatch = new CountDownLatch(3);
    ReentrantLock  lock = new ReentrantLock(false);
    Object objKey = new Object();

    @RequestMapping("/find")
    public List<TaskRule> find(){
        return taskRuleMapper.find();
    }

    @RequestMapping("/batchInsert")
    public void batchInsert(){
        List<User> users = new ArrayList<>();
        long l = System.currentTimeMillis();
        for (int i = 0; i < 100000000; i++) {
            User user = RandomData.random();
            users.add(user);
            if(users.size() >= 100000){
                insertMapper.insertUserBatch(users);
                users.clear();
            }
        }

        if(users.size() > 0){
            insertMapper.insertUserBatch(users);
        }

        System.out.println("使用时间：" + (System.currentTimeMillis() - l));

    }

    @RequestMapping("/importTxt")
    public void importTxt(){
        List<String> fieldNameList = new ArrayList<>();
        //fieldNameList.add("sex");
        fieldNameList.add("id");
        fieldNameList.add("task_name");
        taskRuleMapper.importTxt("task_rule",fieldNameList,"8");
    }

    @RequestMapping("/testException")
    public void testException(){
        StringBuffer sb = new StringBuffer();
        try {
            taskService.testException();
        }catch (TaskException taskException){
            sb.append(taskException.getMessage());

        }
       /* ExcelReader r = new ExcelReader();
        r.getSheetNames();*/
        //return sb.toString();

    }

    @RequestMapping("/ProxyTest")
    public void ProxyTest(){
        ProxyTest proxyTest = new ProxyTest();
        TaskServiceImpl impl = new TaskServiceImpl();
        TaskService bind = (TaskService) proxyTest.bind(impl);
        bind.poxyTest();

        TaskService o = ProxyUtil.newProxyInstance(proxyTest, TaskService.class);
        o.poxyTest();
    }

    @RequestMapping("/AspectTest")
    public void AspectTest(){
        TaskServiceImpl proxy = CglibProxyFactory.createProxy(new TaskServiceImpl(), new AspectTest());

        proxy.poxyTest();
    }

    @RequestMapping("/ThreadTest")
    public void ThreadTest(){
        System.out.println(Thread.currentThread().getName() + "等待执行、、、");
        synchronized (objKey){
            try {
                Thread.sleep(10000);
                System.out.println(Thread.currentThread().getName() + "输出了");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/countDown")
    public void countDown(){

        System.out.println(Thread.currentThread().getName() + "进入方法，开始准备工作");
        countDownLatch.countDown();

        System.out.println(Thread.currentThread().getName() + "准备工作做完");
        try {
            countDownLatch.await();
            System.out.println(Thread.currentThread().getName() + "开始执行业务！");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/LockTest")
    public void LockTest(){
        System.out.println(Thread.currentThread().getName() + "进入方法");
        for (int i = 0; i <3 ; i++) {
            lock.lock();
            try {
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName() + "输出了,目前需等待的线程数为："  + lock.getHoldCount());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            lock.unlock();
        }
    }

    // 2 3 4
}
